#!/usr/bin/env python2.5
# -*- coding: utf-8 -*-
# File: xio2.py
# Date: 2010-06-26
# Author: jyf1987, rex
import web
from outlib.disp import Disp 
import urllib
#import setting
from google.appengine.api import xmpp
#to ensure the utf8 encoding environment
#import sys
#default_encoding = 'utf-8'
#if sys.getdefaultencoding() != default_encoding:
#	reload(sys)
#	sys.setdefaultencoding(default_encoding)


def urlencode(s):
    b = ['%'+'%X' % ord(d) for d in s]
    return ''.join(b)


class XMPPHandler():
    def GET(self):
	    req = web.input()
	    jid = req['jid']
	    user = xmpp.get_presence(jid)
	    if not user:
	        xmpp.send_invite(jid)
	    else:
	        return "user %s is online" % (jid)

    def POST(self):
        req = web.input()
        message = xmpp.Message(req)
#        twclient = Twitter(setting.user,setting.pwd)

        #req["body"] = req["body"]
        #message.reply("Debug:\nbody=%s" % repr(type(req['body'])))
        #twmsg = "%s:"%(req['from'].split('/')[0]) + req["body"]
        twmsg = req["body"]
        disp=Disp(twmsg, message)
        disp.do()


urls = (
	'/_ah/xmpp/message/chat/', XMPPHandler
	)

app = web.application(urls , globals())

if __name__ == "__main__":
  app.cgirun()
